package com.logan.uc.services;

import cn.hutool.core.bean.BeanUtil;
import com.logan.uc.data.dto.UserDTO;
import com.logan.uc.data.form.RegisterForm;
import com.logan.uc.entity.User;
import com.logan.uc.repositorys.UserRepository;
import org.hibernate.tool.schema.ast.SqlScriptParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void save(RegisterForm registerForm) {
        User user = new User();
        BeanUtil.copyProperties(registerForm, user);
        this.userRepository.save(user);
    }

    public <T> T findByUsername(String username, Class<T> classType) {
        T user = this.userRepository.findByUsername(username, classType);
        return user;
    }

    public void physicalDeleteByAccount(String account) {
        User user = this.userRepository.findByUsername(account, User.class);
        this.userRepository.delete(user);
    }

    public void logicalDeleteByAccount(String account) {
        User user = this.userRepository.findByUsername(account, User.class);
        if (user == null) {
            user.setIsDeleted(true);
            this.userRepository.save(user);
        }
    }
}
